﻿using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Module.DataDetail
{
    public partial class ApplyInfor : BaseUI.BasePage
    {
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();
        private Lc_ProcessBLL processBLL = new Lc_ProcessBLL();
        private Lc_TransactionBLL ltb = new Lc_TransactionBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 初始化流程名称

                {
                    //流程不分公司
                    var dt = processBLL.Query("SELECT * FROM LC_PROCESS WHERE N_PROCESS_STATE = 1 ");
                    FORM_PROCESS.DataSource = dt;
                    FORM_PROCESS.DataValueField = "N_PROCESS_ID";
                    FORM_PROCESS.DataTextField = "C_PROCESS_NAME";
                    FORM_PROCESS.DataBind();
                    FORM_PROCESS.Items.Add(new ListItem("---请选择---", "-1"));
                    FORM_PROCESS.SelectedValue = "-1";
                }

                #endregion 初始化流程名称

                QUERY_CLICK(sender, e);
            }
        }

        public void BindData(bool iffirst)
        {
            var sql = @"SELECT *
  FROM (SELECT ROW_NUMBER() OVER(ORDER BY B.N_BUSINESS_ID DESC) R,
               C.CMY_NAME AS COMPANY_NAME,
               C.CMY_ID AS CMY_ID,
               B.N_BUSINESS_ID,
               B.N_BUSINESS_NAME,
               P.C_PROCESS_NAME,
               P.N_PROCESS_ID AS N_PROCESS_ID,
               U.USER_NAME,

               B.N_BUSINESS_OPERATOR_TIME,
               DECODE(B.N_BUSINESS_STATE,
                      0,
                      '注销',
                      1,
                      '办理中',
                      2,
                      '已归档',
                      '其它') AS STATE,
               B.N_REMARK
          FROM QS_BUSINESS B, LC_PROCESS P, SYS_USER U, SYS_COMPANY C
         WHERE B.N_PROCESS_ID = P.N_PROCESS_ID
           AND B.N_COMPANY_ID = C.CMY_ID
           AND B.N_BUSINESS_OPERATOR_ID = U.USER_ID) TT";

            var whereStr = (string)ViewState["WHERE"];
            if (whereStr == null)
            {
                return;
            }
            sql += " WHERE " + whereStr;
            var orderStr = (string)ViewState["ORDER"];
            if (orderStr == null)
            {
                sql += " ORDER BY N_BUSINESS_OPERATOR_TIME DESC";
            }
            else
            {
                sql += " ORDER BY " + orderStr + " " + (string)ViewState["ORDER_TYPE"];
            }
            if (iffirst)
            {
                PageIng.CurrentPage = 1;
                PageIng.RowsCount = companyBLL.SqlCount(sql);
            }
            var dt = companyBLL.PageQuery2(sql, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.DataSource = dt;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.GridViewSource = TABLE;
                PageIng.Visible = true;

                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = TABLE;
                TABLE.DataSource = null;
                TABLE.DataBind();
                PageIng.Visible = false;
            }
        }

        public void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        protected void QUERY_CLICK(object sender, EventArgs e)
        {
            var where = "";
            var cmyid = FORM_COMPANY.Value;
            if (cmyid == -1)
            {
                cmyid = CurrentUserCompanyID;
            }

            var cmyIds = companyBLL.GetCompanyFatherAndChildByID(cmyid);
            if (cmyIds == "")
            {
                MessageBox.Show(Page, "当前登录人公司已经不存在！");
                return;
            }
            where += "CMY_ID IN(" + cmyIds + ")";

            if (FORM_PROCESS.SelectedValue != "-1")
            {
                where += " AND N_PROCESS_ID = " + FORM_PROCESS.SelectedValue;
            }
            if (FORM_STATE.SelectedValue != "-1")
            {
                where += " AND STATE = '" + FORM_STATE.SelectedItem.Text + "'";
            }
            /*
            if (FORM_TYPE.SelectedValue!="-1")
            {
                where += " AND T1.N_BUSINESS_STATE = " + FORM_STATE.SelectedValue;
            }
            */
            if (FORM_NAME.Text != "")
            {
                where += "AND N_BUSINESS_NAME LIKE '%" + FORM_NAME.Text + "%'";
            }
            if (FORM_BEGIN_DATE.Value != "")
            {
                var yeard = FORM_BEGIN_DATE.Value.Split('-');

                where += " AND N_BUSINESS_OPERATOR_TIME >= TO_DATE('" + Convert.ToDateTime(FORM_BEGIN_DATE.Value).ToString("yyyy-MM-dd HH:mm:ss") + "','YYYY-MM-DD HH24:MI:SS')";
            }
            if (FORM_OVER_DATE.Value != "")
            {
                where += " AND N_BUSINESS_OPERATOR_TIME <= TO_DATE('" + Convert.ToDateTime(FORM_OVER_DATE.Value).ToString("yyyy-MM-dd HH:mm:ss") + "','YYYY-MM-DD HH24:MI:SS')";
            }

            ViewState["WHERE"] = where;
            BindData(true);
        }

        #region 打印代码

        protected void PRINT_CLICK(object sender, EventArgs e)
        {
            if ((DataTable)ViewState["database"] == null || ((DataTable)ViewState["database"]).Rows.Count == 0)
            {
                MessageBox.Show(Page, "【系统提示】未能查询出数据，暂时不能打印。");
                return;
            }

            DataTable dt = ((DataTable)ViewState["database"]).Copy();

            dt.Columns["R"].ColumnName = "序号";
            dt.Columns["COMPANY_NAME"].ColumnName = "公司名称";
            dt.Columns["N_BUSINESS_NAME"].ColumnName = "业务名称";
            dt.Columns["C_PROCESS_NAME"].ColumnName = "所属流程";
            dt.Columns["USER_NAME"].ColumnName = "操作人";
            dt.Columns["N_BUSINESS_OPERATOR_TIME"].ColumnName = "申请时间";
            dt.Columns["STATE"].ColumnName = "业务状态";
            dt.Columns.Remove("N_BUSINESS_ID");
            dt.Columns.Remove("CMY_ID");
            dt.Columns.Remove("N_PROCESS_ID");
            dt.Columns.Remove("N_REMARK");
            dt.Columns.Remove("ORACLE_ROW_ID");

            var key = Guid.NewGuid().ToString();
            Session[key + "Header"] = "流程明细";
            Session[key + "Body"] = dt;
            MessageBox.ResponseScript(this, "top.MessageBox.Print({Value:'ID=BASIC&VALUE=" + key + "'})");
        }

        #endregion 打印代码

        protected void SORTING_EVENT(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["ORDER"] == null ? null : (string)ViewState["ORDER"];
            OrderDire = ViewState["ORDER_TYPE"] == null ? null : (string)ViewState["ORDER_TYPE"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["ORDER"] = SortOrder;
            ViewState["ORDER_TYPE"] = OrderDire;
            QUERY_CLICK(sender, e);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            if ((DataTable)ViewState["database"] == null || ((DataTable)ViewState["database"]).Rows.Count == 0)
            {
                MessageBox.Show(Page, "【系统提示】未能查询出数据，暂时不能导出，请先查询数据。");
                return;
            }

            DataTable dt = ((DataTable)ViewState["database"]).Copy();

            dt.Columns["R"].ColumnName = "序号";
            dt.Columns["COMPANY_NAME"].ColumnName = "公司名称";
            dt.Columns["N_BUSINESS_NAME"].ColumnName = "业务名称";
            dt.Columns["C_PROCESS_NAME"].ColumnName = "所属流程";
            dt.Columns["USER_NAME"].ColumnName = "操作人";
            dt.Columns["N_BUSINESS_OPERATOR_TIME"].ColumnName = "申请时间";
            dt.Columns["STATE"].ColumnName = "业务状态";
            dt.Columns.Remove("N_BUSINESS_ID");
            dt.Columns.Remove("CMY_ID");
            dt.Columns.Remove("N_PROCESS_ID");
            dt.Columns.Remove("N_REMARK");
            dt.Columns.Remove("ORACLE_ROW_ID");

            toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "流程明细");
            //toExecl1(Panel1, DateTime.Now.ToString() + "流程明细");
        }
    }
}